#include "pch.hpp"
#include "Application.hpp"

#include <logger/vsstream.hpp>
#include <services/threadpool_scheduler.hpp>



using namespace QtUi;

Application *Application::m_theApp = 0;




Application *Application::instance()
{
	return m_theApp;
}
///////////////////////////////////////////////////////////////////////////////////////////////////




Application::Application(int argc, char** argv)
	: QApplication(argc, argv)
	, m_provider(boost::make_shared<services::provider>())
{
	installTranslator(&m_translator);

	// Load the translation for the current system, for example
	// ":/translation/basic_de"
	QString locale = QLocale::system().name();
	loadTranslation(QString(":/translation/basic_%1").arg(locale));

	m_theApp = this;

	m_provider->registerService(boost::make_shared<logger::vsstream>(m_provider));
	m_provider->registerService(boost::make_shared<services::threadpool_scheduler>(m_provider));
}
///////////////////////////////////////////////////////////////////////////////////////////////////

Application::~Application()
{
	m_theApp = 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////




services::provider::ptr Application::serviceProvider() const
{
	return m_provider;
}
///////////////////////////////////////////////////////////////////////////////////////////////////

void Application::loadTranslation(const QString& fname)
{
	m_translator.load(fname);
}
///////////////////////////////////////////////////////////////////////////////////////////////////




bool Application::notify(QObject* receiver, QEvent* event)
{
	try
	{
		return QApplication::notify(receiver, event);
	}
	catch(const std::exception &except)
	{
		onException(except);
		exit(-1);
		return false; //< dummy
	}
}
///////////////////////////////////////////////////////////////////////////////////////////////////

void Application::onException(const std::exception &except)
{
	QMessageBox msgBox(activeWindow());
	msgBox.setWindowTitle(tr("An exception has been caught"));
	msgBox.setText(tr("An exception causes this application to shut down. I'm sorry for the inconvenience this may cause. Please press Ok to close the Application."));
	msgBox.setInformativeText(tr("The exception is most likely to be a mistake the programmer made. Informing him about the problem will improve the overall quality of this software."));
	msgBox.setDetailedText(QString(tr("Type: std::exception\nMessage: %1")).arg(except.what()));

	msgBox.setIconPixmap(QPixmap(":/ico/warning"));
	msgBox.addButton(QMessageBox::Ok);

	msgBox.exec();
}
///////////////////////////////////////////////////////////////////////////////////////////////////

void Application::onException(const boostext::exception& except)
{
	QMessageBox msgBox(activeWindow());
	msgBox.setWindowTitle(tr("An exception has been caught"));
	msgBox.setText(tr("An exception causes this application to shut down. I'm sorry for the inconvenience this may cause. Please press Ok to close the Application."));
	msgBox.setInformativeText(tr("The exception is most likely to be a mistake the programmer made. Informing him about the problem will improve the overall quality of this software."));
	msgBox.setDetailedText(QString(boost::diagnostic_information(except).c_str()));

	msgBox.setIconPixmap(QPixmap(":/ico/warning"));
	msgBox.addButton(QMessageBox::Ok);

	msgBox.exec();
}
///////////////////////////////////////////////////////////////////////////////////////////////////
